package com.max.mil.fuwu.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.max.mil.common.util.DataPage;
import com.max.mil.fuwu.model.Promotion;
import com.max.mil.fuwu.service.PromotionService;
import com.max.mil.jdbc.AdvanRowProcessor;
import com.max.mil.jdbc.DBToolKit;

public class PromotionServiceImpl implements PromotionService
{

	private static  QueryRunner run = new QueryRunner(DBToolKit.getDataSource());

	@Override
	public int addPromotion(Promotion prom) throws Exception
	{
		int id = -1;
		
		String sql = "INSERT INTO fuwu_promotion(TYPE,NAME,CONTENT,STATUS,EFFECT_TIME,EXPIRE_TIME,CREATE_TIME,MODIFY_TIME) VALUES(?,?,?,?,?,?,?,?)";
		run.update(sql, prom.getType(),prom.getName(),prom.getContent(),prom.getStatus(),prom.getEffectTime(),prom.getExpireTime(),prom.getCreateTime(),prom.getModifyTime());
		id = ((Long)run.query("SELECT LAST_INSERT_ID() AS id", new ScalarHandler("id"))).intValue();
	   
		return id;
	}

	@Override
	public int editPromotion(Promotion prom) throws Exception
	{
		int count = 0;
		
		String sql = "UPDATE fuwu_promotion SET TYPE=?,NAME=?,CONTENT=?,STATUS=?,EFFECT_TIME=?,EXPIRE_TIME=?,CREATE_TIME=?,MODIFY_TIME=? WHERE ID=?";
		count = run.update(sql, prom.getType(),prom.getName(),prom.getContent(),prom.getStatus(),prom.getEffectTime(),prom.getExpireTime(),prom.getCreateTime(),prom.getModifyTime(),prom.getId());
	   
		return count;
	}

	@Override
	public Promotion getPromotion(int id) throws Exception
	{
		Promotion prom = null;
		prom = (Promotion) run.query("SELECT * FROM fuwu_promotion WHERE ID=?", 
				new BeanHandler(Promotion.class, new AdvanRowProcessor()), id);
		
		return prom;
	}

	@SuppressWarnings("unchecked")
	@Override
	public DataPage<Promotion> getPromotion(int type, int status, int pageNo, int pageSize)
			throws Exception
	{
		int totalCount = 0;
		List<Promotion> list = null;

		Date currTime = new Date();
		
		if(status == 11) 
		{
			totalCount = ((Long)run.query("SELECT COUNT(1) FROM fuwu_promotion WHERE TYPE=? AND STATUS=1 AND EFFECT_TIME>? ORDER BY ID DESC", 
					new ScalarHandler(1), type, currTime)).intValue();
			list = (List<Promotion>)run.query("SELECT * FROM fuwu_promotion WHERE TYPE=? AND STATUS=1 AND EFFECT_TIME>? ORDER BY ID DESC LIMIT ?,?", 
					new BeanListHandler(Promotion.class, new AdvanRowProcessor()), type, currTime, (pageNo-1)*pageSize, pageSize);
		}
		else if(status == 12) 
		{
			totalCount = ((Long)run.query("SELECT COUNT(1) FROM fuwu_promotion WHERE TYPE=? AND STATUS=1 AND ? BETWEEN EFFECT_TIME AND EXPIRE_TIME ORDER BY ID DESC", 
					new ScalarHandler(1), type, currTime)).intValue();
			list = (List<Promotion>)run.query("SELECT * FROM fuwu_promotion WHERE TYPE=? AND STATUS=1 AND EFFECT_TIME>? ORDER BY ID DESC LIMIT ?,?", 
					new BeanListHandler(Promotion.class, new AdvanRowProcessor()), type, currTime, (pageNo-1)*pageSize, pageSize);
		}
		else if(status == 13) 
		{
			totalCount = ((Long)run.query("SELECT COUNT(1) FROM fuwu_promotion WHERE TYPE=? AND STATUS=1 AND EXPIRE_TIME<? ORDER BY ID DESC", 
					new ScalarHandler(1), type, currTime)).intValue();
			list = (List<Promotion>)run.query("SELECT * FROM fuwu_promotion WHERE TYPE=? AND STATUS=1 AND EFFECT_TIME>? ORDER BY ID DESC LIMIT ?,?", 
					new BeanListHandler(Promotion.class, new AdvanRowProcessor()), type, currTime, (pageNo-1)*pageSize, pageSize);
		}
		else
		{
			totalCount = ((Long)run.query("SELECT COUNT(1) FROM fuwu_promotion WHERE TYPE=? AND STATUS=? ORDER BY ID DESC", 
					new ScalarHandler(1), type, status)).intValue();
			list = (List<Promotion>)run.query("SELECT * FROM fuwu_promotion WHERE TYPE=? AND STATUS=? ORDER BY ID DESC LIMIT ?,?", 
					new BeanListHandler(Promotion.class, new AdvanRowProcessor()), type, status, (pageNo-1)*pageSize, pageSize);
		}
		
		return new DataPage(list, pageNo, pageSize, totalCount);
	}
	
	
	public static void main(String[] args) throws Exception
	{
		PromotionService service = new PromotionServiceImpl();
		
		
		for(int i=1;i<200;i++)
		{
			Promotion prom = new Promotion();
			prom.setType(1);
			prom.setName("野百合也有春天");
			prom.setContent("野百合也有春天野百合也有春天野百合也有春天野百合也有春天");
			prom.setStatus(1);
			prom.setEffectTime(new Date());
			prom.setExpireTime(new Date());
			prom.setCreateTime(new Date());
			prom.setModifyTime(new Date());
			
			prom.setName(prom.getName()+i);
			int ri = new Random().nextInt(30);
			prom.setEffectTime(new Date(System.currentTimeMillis()+(1000*60*60*24*(15-ri))));
			prom.setExpireTime(new Date(prom.getEffectTime().getTime()+1000*60*60*24));
			System.out.println(service.addPromotion(prom));
		}
		
//		prom = service.getPromotion(4);
//		System.out.println(prom.getName());
//		prom.setName("xxxxxxxxx");
//		System.out.println(service.editPromotion(prom));
//		System.out.println(service.getPromotion(1, 2, 1, 10));
	}
}
